package backend.project.gym_management.controller;

import backend.project.gym_management.base.response.JsonResult;
import backend.project.gym_management.base.response.StatusCode;
import backend.project.gym_management.pojo.dto.*;
import backend.project.gym_management.service.AdminService;
import com.github.pagehelper.PageInfo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 管理员功能模块
 */
@Slf4j
@RestController
@RequestMapping("/v1/admin/")
@Api(tags = "02.管理员功能模块")
public class AdminController {
    @Autowired
    private AdminService adminService;

    /**
     * 查询数据表功能
     * @param adminQuery
     * @return JsonResult
     */
    @PostMapping("query")
    @ApiOperation("查询表数据")
    @ApiOperationSupport(order = 10)
    public JsonResult query(@RequestBody AdminQuery adminQuery){
        log.debug("开始处理查询表数据请求，参数：{}", adminQuery);
        List<?> result = adminService.query(adminQuery);
        if (result != null) {
            log.debug("查询表数据成功，结果：{}", result);
            return JsonResult.ok(result);
        } else
            return new JsonResult(StatusCode.INVALID_PARAMETER, "未找到相关用户信息");
    }


    /**
     * 删除表数据功能
     * @param adminDelete
     * @return JsonResult
     */
    @DeleteMapping("delete")
    @ApiOperation("删除表数据")
    @ApiOperationSupport(order = 20)
    public JsonResult delete(@RequestBody @Valid AdminDeleteParam adminDelete){
        log.debug("开始处理删除表数据请求，参数：{}", adminDelete);
        boolean result = adminService.delete(adminDelete);
        if(result){
            log.debug("删除表数据成功");
            return JsonResult.ok("删除成功");
        }else {
            log.debug("删除表数据失败");
            return new JsonResult(StatusCode.INVALID_PARAMETER, "删除失败");
        }
    }
    /**
     * 新增表数据功能
     */
    @PostMapping("add")
    @ApiOperation("新增表数据")
    @ApiOperationSupport(order = 30)
    public JsonResult add(@RequestBody @Valid AdminAdd adminAdd) {
        log.debug("开始处理新增表数据请求，参数：{}", adminAdd);
        boolean result = adminService.add(adminAdd);
        if(result){
            log.debug("新增表数据成功");
            return JsonResult.ok("添加成功");
        }else {
            log.debug("新增表数据失败");
            return new JsonResult(StatusCode.INVALID_PARAMETER, "新增失败");
        }
    }

    /**
     * 修改表数据功能
     */
    @PutMapping("update")
    @ApiOperation("修改表数据")
    @ApiOperationSupport(order = 40)
    public JsonResult update(@RequestBody @Valid AdminUpdate adminUpdate) {
        log.debug("开始处理修改表数据请求，参数：{}", adminUpdate);
        boolean result = adminService.update(adminUpdate);
        if(result){
            log.debug("修改表数据成功");
            return JsonResult.ok("修改成功");
        }else {
            log.debug("修改表数据失败");
            return new JsonResult(StatusCode.INVALID_PARAMETER, "修改失败");
        }
    }
    @GetMapping("/selectPage")
public JsonResult selectPage(@RequestParam(defaultValue = "1") Integer pageNum,
                            @RequestParam(defaultValue = "10") Integer pageSize,
                            AdminQuery adminQuery) {
    log.debug("开始处理分页查询请求，参数：{}", adminQuery);
    PageInfo<?> pageInfo = adminService.selectPage(adminQuery, pageNum, pageSize);
    log.debug("分页查询成功，总记录数：{}", pageInfo.getTotal());
    return JsonResult.ok(pageInfo);
}
@DeleteMapping("batchDelete")
@ApiOperation("批量删除表数据")
@ApiOperationSupport(order = 50)
public JsonResult batchDelete(@RequestBody @Valid AdminBatchDeleteParam adminBatchDelete) {
    log.debug("开始处理批量删除表数据请求，参数：{}", adminBatchDelete);
    boolean result = adminService.batchDelete(adminBatchDelete);
    if (result) {
        log.debug("批量删除表数据成功");
        return JsonResult.ok("批量删除成功");
    } else {
        log.debug("批量删除表数据失败");
        return new JsonResult(StatusCode.INVALID_PARAMETER, "批量删除失败");
    }

}
@GetMapping("/checkCidExist")
@ApiOperation("检查课程是否存在")
public JsonResult checkCidExist(@RequestParam Integer cid) {
    log.debug("开始检查课程是否存在，cid={}", cid);
    boolean exists = adminService.checkCidExist(cid);
    return JsonResult.ok(exists);
}
@GetMapping("/checkIntroductionExist")
@ApiOperation("检查课程简介是否存在")
public JsonResult checkIntroductionExist(@RequestParam Integer cid) {
    boolean exists = adminService.checkIntroductionExist(cid);
    return JsonResult.ok(exists);
}

    // 检查eid是否为“教练”
    @GetMapping("/checkCoachExist")
    public ResponseEntity<?> checkCoachExist(@RequestParam Integer eid) {
        boolean exists = adminService.checkCoachExist(eid);
        return ResponseEntity.ok().body(Map.of("state", 2001, "data", Map.of("exists", exists)));
    }

    @GetMapping("/checkCoachIntroductionExist")
    public ResponseEntity<?> checkCoachIntroductionExist(@RequestParam Integer eid) {
        boolean exists = adminService.checkCoachIntroductionExist(eid);
        return ResponseEntity.ok().body(Map.of("state", 2001, "data", Map.of("exists", exists)));
    }
    @GetMapping("/checkEquipmentExist")
    public ResponseEntity<?> checkEquipmentExist(@RequestParam Integer seid) {
        boolean exists = adminService.checkEquipmentExist(seid);
        return ResponseEntity.ok().body(Map.of("state", 2001, "data", Map.of("exists", exists)));
    }

    @GetMapping("/checkRepairStatusExist")
    public ResponseEntity<?> checkRepairStatusExist(@RequestParam Integer seid) {
        boolean exists = adminService.checkRepairStatusExist(seid);
        return ResponseEntity.ok().body(Map.of("state", 2001, "data", Map.of("exists", exists)));
    }
    @GetMapping("/checkUidExist")
@ApiOperation("检查用户是否存在")
public JsonResult checkUidExist(@RequestParam Integer uid) {
    boolean exists = adminService.checkUidExist(uid);
    return JsonResult.ok(exists);
}

}





